package com.healthtop.utils;


import com.healthtop.dto.MenuDto;
import com.healthtop.dto.SessionUserDto;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Administrator on 2016/10/16.
 */
public class UserUtils {

    public static final String CURRENT_USER = "current_user";

    public static final String MENUS = "menus";

    public static final String WELCOME_IMAGE = "welcomeImage";

    public static SessionUserDto getCurrentUser(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return (SessionUserDto) request.getSession().getAttribute(CURRENT_USER);
    }

    public static void setCurrentUser(SessionUserDto user){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        request.getSession().setAttribute(CURRENT_USER,user);
    }

    public static void setMenus(List<MenuDto> menus) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        request.getSession().setAttribute(MENUS,menus);
    }

    public static void cleanSession(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        request.getSession().invalidate();
    }

    public static void setWelcomeImage(String welcomeImage){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        request.getSession().setAttribute(WELCOME_IMAGE,welcomeImage);
    }

}
